import dbus
import dbus.mainloop.glib
import simplejson as json
import gobject
import time

def time_ymd():
    return time.strftime("%Y%m%d", time.localtime())

def save(data):
    t = time_ymd()
    fn = "/home/user/MyDocs/cellid" + t + ".json"
    c = json.dumps(data)
    try:
        f = open(fn,'a')
        f.write(c+'\n')
        f.close()
    except:
        print "Error..."

def cell_signal_handler(*args):
    now = time.time()
    cell = {'t': now,
        'cid': args[2], 
        'lac': args[1], 
        'mnc': args[3], 
        'mcc': args[4],
        'dbm': False,
    }
    print args[2]
    save(cell)

if __name__ ==  "__main__":

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()
    #bus.add_signal_receiver(self.cell_signal_handler, dbus_interface = "Phone.Net", signal_name = "registration_status_change")
    bus.add_signal_receiver(cell_signal_handler, dbus_interface = "Phone.Net", signal_name = "cell_info_change")

    loop = gobject.MainLoop()
    loop.run()
